package com.amlyc.demo2;

import com.amlyc.demo1.TestThread2;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;

public class TestCallable implements Callable<Boolean> {


    private String url;
    private String name;

    public TestCallable(String url, String name) {
        this.url=url;
        this.name=name;
    }

    @Override
    public Boolean call() throws Exception{
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("name:"+name);
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable testThread1 = new TestCallable("https://blog.kuangstudy.com/usr/themes/PureLoveForTypecho/images/banner1.jpg", "1.jpg");
        TestCallable testThread2 = new TestCallable("https://blog.kuangstudy.com/usr/themes/PureLoveForTypecho/images/banner1.jpg", "2.jpg");
        TestCallable testThread3 = new TestCallable("https://blog.kuangstudy.com/usr/themes/PureLoveForTypecho/images/banner1.jpg", "3.jpg");
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        Future<Boolean> s1 = executorService.submit(testThread1);
        Future<Boolean> s2 = executorService.submit(testThread2);
        Future<Boolean> s3 = executorService.submit(testThread3);
        Boolean a = s1.get();
        Boolean b = s2.get();
        Boolean c = s3.get();
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        executorService.shutdownNow();
    }
}
class WebDownloader{

    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
